package cn.com.blueInfo.bpm.template.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.template.entity.ProcessNode;
import cn.com.blueInfo.bpm.template.enums.ProcessNodeDraftFlag;
import cn.com.blueInfo.bpm.template.enums.ProcessNodeType;
import cn.com.blueInfo.bpm.template.mapper.ProcessNodeMapper;
import cn.com.blueInfo.bpm.template.repository.ProcessNodeRepository;
import cn.com.blueInfo.bpm.template.request.ProcessNodeDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 模板节点定义表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class ProcessNodeRepositoryImpl
        extends ServiceImpl<ProcessNodeMapper, ProcessNode>
        implements ProcessNodeRepository, BaseRepository<ProcessNode, ProcessNodeDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增模板节点定义
     * @param processNode 模板节点定义参数
     */
    @Override
    public Integer createProcessNode(ProcessNode processNode) {
        return sqlExecuteResult(save(processNode));
    }

    /**
     * 批量新增模板节点定义
     * @param processNodeList 模板节点定义参数列表
     */
    @Override
    public Integer batchCreateProcessNode(List<ProcessNode> processNodeList) {
        return sqlExecuteResult(saveBatch(processNodeList), processNodeList.size());
    }

    /**
     * 删除模板节点定义
     * @param processNodeId 模板节点定义ID
     */
    @Override
    public Integer deleteProcessNode(Long processNodeId) {
        ProcessNode processNode = new ProcessNode();
        processNode.setId(processNodeId);
        processNode.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(processNode));
    }

    /**
     * 批量删除模板节点定义
     * @param processNodeIdList 模板节点定义ID列表
     */
    @Override
    public Integer batchDeleteProcessNode(List<Long> processNodeIdList) {
        List<ProcessNode> processNodeList = new ArrayList<>();
        for (Long processNodeId : processNodeIdList) {
            ProcessNode processNode = new ProcessNode();
            processNode.setId(processNodeId);
            processNode.setDeleted(BaseDeleted.DELETED);
            processNodeList.add(processNode);
        }
        return sqlExecuteResult(removeBatchByIds(processNodeList), processNodeList.size());
    }

    /**
     * 更新部分模板节点定义
     * @param processNode 模板节点定义参数
     */
    @Override
    public Integer updateProcessNodePartial(ProcessNode processNode) {
        validateUpdateId(processNode.getId());
        LambdaUpdateWrapper<ProcessNode> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ProcessNode::getId, processNode.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部模板节点定义
     * @param processNode 模板节点定义参数
     */
    @Override
    public Integer updateProcessNode(ProcessNode processNode) {
        validateUpdateId(processNode.getId());
        return sqlExecuteResult(updateById(processNode));
    }

    /**
     * 查询一条模板节点定义
     * @param processNodeId 模板节点定义ID
     */
    @Override
    public ProcessNode getProcessNodeById(Long processNodeId) {
        LambdaQueryWrapper<ProcessNode> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcessNode::getId, processNodeId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询模板节点定义
     * @param processNodeQueryDTO 模板节点定义参数
     */
    @Override
    public Page<ProcessNode> getProcessNodePage(ProcessNodeDTO.QueryDTO processNodeQueryDTO) {
        Page<ProcessNode> processNodePage = new Page<>(
                processNodeQueryDTO.getPage(), processNodeQueryDTO.getSize());
        LambdaQueryWrapper<ProcessNode> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, processNodeQueryDTO);

        BaseDeleted deleted = processNodeQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(processNodePage, queryWrapper);
        } else {
            return page(processNodePage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 模板节点定义 数量
     * @param processNodeQueryDTO 模板节点定义参数
     */
    @Override
    public Long getProcessNodeCount(ProcessNodeDTO.QueryDTO processNodeQueryDTO) {
        LambdaQueryWrapper<ProcessNode> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, processNodeQueryDTO);

        BaseDeleted deleted = processNodeQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 模板节点定义
     * @param processNodeQueryDTO 模板节点定义参数
     */
    @Override
    public List<ProcessNode> getProcessNodeList(ProcessNodeDTO.QueryDTO processNodeQueryDTO) {
        LambdaQueryWrapper<ProcessNode> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, processNodeQueryDTO);

        BaseDeleted deleted = processNodeQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<ProcessNode> queryWrapper,
                              ProcessNodeDTO.QueryDTO processNodeQueryDTO) {

        Long processId = processNodeQueryDTO.getProcessId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(processId), ProcessNode::getProcessId, processId);

        String nodeCode = processNodeQueryDTO.getNodeCode();
        queryWrapper.eq(StringUtils.isNotBlank(nodeCode), ProcessNode::getNodeCode, nodeCode);

        String nodeName = processNodeQueryDTO.getNodeName();
        queryWrapper.eq(StringUtils.isNotBlank(nodeName), ProcessNode::getNodeName, nodeName);

        ProcessNodeType nodeType = processNodeQueryDTO.getNodeType();
        queryWrapper.eq(ObjectUtils.isNotEmpty(nodeType), ProcessNode::getNodeType, nodeType);

        ProcessNodeDraftFlag draftFlag = processNodeQueryDTO.getDraftFlag();
        queryWrapper.eq(ObjectUtils.isNotEmpty(draftFlag), ProcessNode::getDraftFlag, draftFlag);

        String participantRule = processNodeQueryDTO.getParticipantRule();
        queryWrapper.eq(StringUtils.isNotBlank(participantRule), ProcessNode::getParticipantRule, participantRule);

        Long formId = processNodeQueryDTO.getFormId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(formId), ProcessNode::getFormId, formId);

        String nodeLoadFunc = processNodeQueryDTO.getNodeLoadFunc();
        queryWrapper.eq(StringUtils.isNotBlank(nodeLoadFunc), ProcessNode::getNodeLoadFunc, nodeLoadFunc);

        String clickNodeFunc = processNodeQueryDTO.getClickNodeFunc();
        queryWrapper.eq(StringUtils.isNotBlank(clickNodeFunc), ProcessNode::getClickNodeFunc, clickNodeFunc);

        String extendSign = processNodeQueryDTO.getExtendSign();
        queryWrapper.eq(StringUtils.isNotBlank(extendSign), ProcessNode::getExtendSign, extendSign);

        addQueryWrapper(queryWrapper, processNodeQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<ProcessNode, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                ProcessNode::getOrderNumber,
                ProcessNode::getTenantId,
                ProcessNode::getApplicationId,
                ProcessNode::getStatus,
                ProcessNode::getDeleted,
                ProcessNode::getDeleteUserId,
                ProcessNode::getDeleteTime,
                ProcessNode::getRemark,
                ProcessNode::getCreateUserId,
                ProcessNode::getCreateTime,
                ProcessNode::getUpdateUserId,
                ProcessNode::getUpdateTime,
                ProcessNode::getExtend1,
                ProcessNode::getExtend2,
                ProcessNode::getExtend3);
    }

}
